package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.EquipmentException;
import com.uzhie.exception.VehicleException;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.Equipment;
import com.uzhie.web.module.Vehicle;
import com.uzhie.web.service.EquipmentService;
import com.uzhie.web.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by TangFei on 2018/8/21.
 * 快速操作控制器
 */
@RestController
@RequestMapping(value = "quick")
public class QuickOperController extends CommController {

    @Autowired
    @SuppressWarnings(value = "SpringJavaAutowiringInspection")
    private VehicleService vehicleService;
    @Autowired
    @SuppressWarnings(value = "SpringJavaAutowiringInspection")
    private EquipmentService equipmentService;

    /**
     * 快速添加车辆设备信息
     *
     * @param requestBody
     * @return
     */
    @RequestMapping(value = "addVehicleAndDev", method = RequestMethod.POST)
    public PayloadBean addVehicleAndDev(@RequestBody(required = false) JSONObject requestBody) {
        try {

            Vehicle vehicle = JSONObject.toJavaObject(requestBody, Vehicle.class);
            Equipment equipment = JSONObject.toJavaObject(requestBody, Equipment.class);
            vehicle.setSimNumber(equipment.getSimNumber());
            vehicle.setDeviceNumber(equipment.getDeviceNumber());
            vehicleService.addVehicle(vehicle);
            equipment.setDepartId(vehicle.getDepartId());
            equipment.setDepartName(vehicle.getDepartName());
            equipmentService.addEquipment(equipment);
            equipmentService.bindVehicles(equipment.getId(), vehicle.getId());
            return buildResponse(null);
        } catch (VehicleException e) {
            e.printStackTrace();
            logger.error("添加车辆信息错误,请查看车牌号等关键信息是否重复");
            return buildResponse(null, e.getMessage());
        } catch (EquipmentException e) {
            e.printStackTrace();
            logger.error("添加设备信息错误,请查看sim卡号等关键信息是否重复");
            return buildResponse(null, e.getMessage());
        }
    }

}
